#!/bin/sh
a=1
DEVICE='00:25:47:09:95:67'
BOUNDS=-15
DELAY=2.5
chmod +s /usr/bin/hcitool
connected=0
function checkcon
{
	num=$(hcitool con |grep $DEVICE -c)	#Filtering $DEVICE from the Active connections and counting the lines returned
	if [ $num == 1 ]; then	#If a connection exists to that device.
		connected=1
	elif [ -z "`hcitool cc $DEVICE 2>&1 sed 's/Scanning/d'`" ]; then	#if Connection to that device is successful.
		connected=1
	fi
}
echo "Proximity Detector"
checkcon	#Calling the checkcon function
while [ $connected == 0 ]; do	#Looping and calling checkcon, till connection is established
	checkcon
done
while /bin/true; do
	checkcon
	if [ $connected == 1 ]; then
        	signal=$(hcitool rssi $DEVICE |awk '{print int($4)}')
		if [ $signal -gt $BOUNDS ]; then
			echo "HERE"	#can also execute an action
		else
			echo "OUT"	#can also execute an action
		fi
	fi
	sleep $DELAY
done
